<?php

namespace App\Http\Controllers\API;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Article;
use App\Model\ArticleCategory;
use App\Model\Product;
use App\Model\ProductCategory;
use App\Model\Ad;
use App\Model\AdCategory;
use App\Model\About;
use App\Model\Feedback;

class WechatAppController extends Controller
{
    /*
     * 文章
     */
    // 获取文章列表(含分页)
    public function article_list($pagination = 0) {
        $page = ($pagination - 1) * 10;
        $articles = Article::skip($page)->take(10)->get();
        $count = Article::count();
        $pageCount = (int)ceil($count/10);
        $articles[0]['pageCount'] = $pageCount;
        if($articles) {
            $data = array('list' => $articles);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取文章详情
    public function article_detail($id) {
        $article = Article::whereId($id)->get();
        if($article) {
            $data = array('list' => $article);
              return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取文章搜索结果
    public function article_search($keyword) {
        $articles = Article::where('name', 'like', '%'.$keyword.'%')->get();
        //dump($articles);
        $data = array('list' => $articles);
        return $this->ReturnJson(true, '', $data);
    }
    // 获取文章分类
    public function article_category() {
        $categorys = ArticleCategory::get();
        //dump($categorys);
        $data = array('list' => $categorys);
        return $this->ReturnJson(true, '', $data);
    }
    // 获取分类文章列表
    public function article_category_list($id,$pagination = 0) {
        $page = ($pagination - 1) * 10;
        $articles = Article::whereCategory_id($id)->skip($page)->take(10)->get();
        $count = Article::whereCategory_id($id)->count();
        $pageCount = (int)ceil($count/10);
        $articles[0]['pageCount'] = $pageCount;
        if($articles) {
            $data = array('list' => $articles);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }

    /*
     * 产品
     */
    // 获取产品列表
    public function product_list($pagination = 0) {
        $page = ($pagination - 1) * 10;
        $products = Product::skip($page)->take(10)->get();
        $count = Product::count();
        $pageCount = (int)ceil($count/10);
        $products[0]['pageCount'] = $pageCount;
        if($products) {
            $data = array('list' => $products);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取产品详情
    public function product_detail($id) {
        $product = Product::whereId($id)->get();
        if($product) {
            $data = array('list' => $product);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取产品搜索结果
    public function product_search($keyword) {
        $products = Product::where('name', 'like', '%'.$keyword.'%')->get();
        $data = array('list' => $products);
        return $this->ReturnJson(true, '', $data);
    }
    // 获取产品分类
    public function product_category() {
        $categorys = ProductCategory::get();
        $data = array('list' => $categorys);
        return $this->ReturnJson(true, '', $data);
    }
    // 获取分类产品列表
    public function product_category_list($id, $pagination = 0) {
        $page = ($pagination - 1) * 10;
        $products = Product::whereCategory_id($id)->skip($page)->take(10)->get();
        $count = Product::whereCategory_id($id)->count();
        $pageCount = (int)ceil($count/10);
        $products[0]['pageCount'] = $pageCount;
        if($products) {
            $data = array('list' => $products);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }

    /*
     * 广告
     */
    // 获取广告列表
    public function ad() {
        $ads = Ad::get();
        if($ads) {
            $data = array('list' => $ads);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取广告详情
    public function ad_detail($id) {
        $ad = Ad::whereId($id)->get();
        if($ad) {
            $data = array('list' => $ad);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取广告分类
    public function ad_category() {
        $categorys = AdCategory::get();
        if($categorys) {
            $data = array('list' => $categorys);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取广告产品列表
    public function ad_category_list($id) {
        $ads = Ad::whereCategory_id($id)->get();
        if($ads) {
            $data = array('list' => $ads);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }

    /*
     * 关于我们
     */
    // 获取关于我们列表，详情
    public function about($id = null) {
        if($id) {
            $about = About::whereId($id)->get();
            if($about) {
                $data = array('list' => $about);
                return $this->ReturnJson(true, '', $data);
            }
            else {
                return $this->ReturnJson(false, '数据读取错误');
            }
        }
        else {
            $list = About::get();
            if($list) {
                $data = array('list' => $list);
                return $this->ReturnJson(true, '', $data);
            }
            else {
                return $this->ReturnJson(false, '数据读取错误');
            }
        }
    }
    /*
     * 留言反馈
     */
    // 获取留言反馈列表，单条留言反馈
    public function feedback($id = null) {
        if($id) {
            $feedback = Feedback::whereId($id)->get();
            if($feedback) {
                $data = array('list' => $feedback);
                return $this->ReturnJson(true, '', $data);
            }
            else {
                return $this->ReturnJson(false, '数据读取错误');
            }
        }
        else {
            $feedback = Feedback::get();
            if($feedback) {
                $data = array('list' => $feedback);
                return $this->ReturnJson(true, '', $data);
            }
            else {
                return $this->ReturnJson(false, '数据读取错误');
            }
        }
    }
    //接受留言反馈
    public function feedback_post(Request $request) {

    }

    /*
     * 用户
     */
    // 获取用户信息
    public function get_user($id) {
        $user = User::whereId($id)->get();
        if($user) {
            $data = array('list' => $user);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }
    // 获取用户留言
    public function user_fb($id) {
        $feedback = Feedback::whereCreate_user($id)->get();
        if($feedback) {
            $data = array('list' => $feedback);
            return $this->ReturnJson(true, '', $data);
        }
        else {
            return $this->ReturnJson(false, '数据读取错误');
        }
    }











    // 数组转JSON
    private function ReturnJson($success,$errorMsg,$content){
        print_r(json_encode([
            'success'=>$success,
            'errorMsg'=>$errorMsg,
            'content'=>$content,
        ]));
        die();
    }
}
